/*
 * Copyright (c) 2011, Yawning <yawninglol at gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   * Neither the name of the Mew Developers nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package yawning.mew.cat;

import yawning.mew.cat.CatModelSimulation.SimulatorProperty;
import yawning.mew.sim.SimulationEvent;

public class EventTranquility extends SimulationEvent {
	CatModelSimState mState;
	private double mStartTime;
	private boolean mIsCasting;

	protected EventTranquility(CatModelSimState state) {
		super(state.mSimEngine);
		mState = state;
		if (mState.mSimEngine.getConfig().getBool(SimulatorProperty.TRANQUILITY)) {
			// Schedule the Tranquility.

			if (mState.mSimEngine.getConfig().getBool(SimulatorProperty.REBIRTH)) {
				// Sigh, need to schedule this around the Rebirth.
				double rebirthStart = mState.mRebirthEv.getExpiryTime();
				double rebirthEnd = rebirthStart + 2 + 1.5; // Base cast + GCD
				boolean canScheduleBefore = (rebirthStart > 8 + 1.5);
				boolean canScheduleAfter = (mState.mSimEngine.getEndTime() - rebirthEnd > 8 + 1.5);

				boolean before = mState.mSimEngine.getRandom().nextBoolean();
				if (!canScheduleAfter || (before && canScheduleBefore)) {
					mStartTime = (rebirthStart - (8 + 1.5)) * mState.mSimEngine.getRandom().nextDouble();
				} else if (canScheduleAfter) {
					mStartTime = rebirthEnd + (mState.mSimEngine.getEndTime() - rebirthEnd) * mState.mSimEngine.getRandom().nextDouble();
				} else {
					// Note: The UI should ensure that the encounter duration is sufficiently long.
					mState.mSimEngine.getView().onError("Failed to schedule Tranquility, insufficient encounter duration?");
					return;
				}
			} else {
				// XXX: Should I enforce a grace period at the beginning/end of the fight?
				// double gracePeriod = mState.mSimUtil.getEndTime() * 0.1;
				// double when = gracePeriod + (mState.mSimUtil.getEndTime() - gracePeriod) * mState.mSimUtil.getRandom().nextDouble();

				// Note: Fudge the numbers so that the cast will always finish.
				mStartTime = (mState.mSimEngine.getEndTime() - (8 + 1.5)) * mState.mSimEngine.getRandom().nextDouble();
			}

			mIsCasting = false;

			schedule(mStartTime);
		}
	}

	@Override
	public void onExpire() {
		if (mIsCasting == false) {
			// Cast the Tranquility.
			if (mState.mIsLogging) mState.mLog.log("Tranquility: Cast Start\n");

			mIsCasting = true;

			// Wipe Strength of the Panther.
			mState.mTier_11_4pcEv.cancel();

			double castTime = 8.0;

			// Adjust for Spell Haste.
			castTime = castTime / mState.mModel.getSpellHaste();
			castTime = Math.max(1.0, castTime);	// Caps out at 1.0 seconds (Cast time = GCD hard cap).

			// Stop the swing timer.
			mState.mMeleeSwingEv.stop();

			schedule(castTime);
		} else {
			// Shift to cat.
			mState.mEnergyTickEv.onCatShift();
			mIsCasting = false;

			if (mState.mIsLogging) mState.mLog.log("Tranquility: Cast Complete\n");

			mState.mSimEngine.triggerGCD(mState.mModel.getShiftGCD());
		}
	}

	public double getStartTime() {
		return mStartTime;
	}

	public boolean isCasting() {
		return mIsCasting;
	}
}
